const express = require("express");
var wechat = require("wechat");
var fly = require("flyio");
const app = express();
var config = {//config for for Wetchat Official Accounts
  token: "",
  appid: "",
  encodingAESKey: "",
  checkSignature: true
};
var tr_url = "http://www.tuling123.com/openapi/api";
var tr_key = "";//turing api key
app.use(express.query());
app.use('/', wechat(config, function(req, res, next) {
  console.log("recive");
  console.log(req.weixin);
  if (req.weixin.MsgType != "text") return -1;
  var msg = req.weixin.Content;
  var id = req.weixin.FromUserName;
  var rmsg = "";
  fly.post(tr_url, {
    "key": tr_key,
    "info": msg,
    "userid": id
  }).then(function(req) {
    var rjson = eval("(" + req.data + ")");
    rmsg = rjson.text.toString();
    console.log(rmsg);
    res.reply({
      content: rmsg,
      type: 'text'
    });
    console.log("reply");
  }).catch(function(error) {
    console.log(error);
  });
}));
const port = 5050;
app.listen(port);
console.log(`Server listening at ${port}`);
